package com.tencent.iov.cowin.befsale.clue.service.cus.impl;

import com.github.pagehelper.Page;
import com.tencent.iov.cowin.befsale.clue.controller.cus.assembler.CustomerExtAssembler;
import com.tencent.iov.cowin.befsale.clue.domain.cus.po.CustomerExt;
import com.tencent.iov.cowin.befsale.clue.repository.cus.CustomerExtManager;
import com.tencent.iov.cowin.befsale.clue.service.cus.CustomerExtService;
import com.software.boot.common.exception.BusinessException;
import com.software.boot.common.exception.NEError;
import com.software.boot.common.model.dao.PageRequest;
import com.software.boot.common.model.dao.PageResponse;
import com.software.boot.common.util.BeanUtil;
import com.software.boot.common.util.EmptyUtils;
import com.software.boot.common.util.ValidateUtils;
import com.software.common.config.SearchLocalHeader;
import com.software.common.enums.DeleteEnum;
import com.software.common.enums.SCRMRequestHeaderEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Describe: 售前成员字典值扩展表_Service实现层
 * @author: xiamu
 * @created: 2021-12-29 10:33:50
 * @version: 1.0
 */
@Service
public class CustomerExtServiceImpl implements CustomerExtService {
	
	@Autowired
	private CustomerExtManager customerExtManager;

	/**
	 * 通过ID获取详细信息
	 *
	 * @param: request
	 * @return: CustomerExt对象
	 */
	@Override
	public CustomerExt getDetailById(Long id){
		/** 1 验证请求参数 */
		ValidateUtils.checkObjNull(id, "请求参数不能为空");
		ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
		/** 2 处理业务 */
		CustomerExt customerExt = customerExtManager.getDetailById(id);
		if (EmptyUtils.isNotEmpty(customerExt)) {
			return BeanUtil.copy(customerExt, CustomerExt.class);
		}
		/** 3 返回结果 */
		return null;
	}

	/**
	 * 单条数据插入CustomerExt
	 * @param request
	 * @return 插入的条数
	 */
	@Override
	public int insert (CustomerExt request){
		/** 1 验证请求参数 */
		/** 2 处理业务 */
		int count = customerExtManager.insert(request);
		/** 3 封装返回值 */
		return count;
    }

	/**
	 * 批量插入CustomerExt
	 * @param requests
	 * @return 批量插入的条数
	 */
	@Override
    public int insertBatch(List<CustomerExt> requests){
		/** 1 验证请求参数 */
		/** 2 处理业务 */
		int count = customerExtManager.insertBatch(requests);
		/** 3 封装返回值 */
		return count;
    }


	/**
	 * 单条CustomerExt更新
	 * @param request
	 * @return 更新的条数
	 */
	@Override
    public int update(CustomerExt request){
	
		/** 1 验证请求参数 */
		/** 2 处理业务 */
		int count = customerExtManager.update(request);
		/** 3 返回对象 */
		return count;
    }

	/**
	 * 通过ID删除数据
	 * @param request
	 * @return 删除的条数
	 */
	@Override
	public int delete(CustomerExt request){
		Long id = request.getId();
		/** 1 验证请求参数 */
		ValidateUtils.checkObjNull(id, "请求参数不能为空");
		ValidateUtils.checkIntNotNullAndGTZero(id.intValue(), "ID不能为空或小于0");
		/** 2 处理业务 */
		CustomerExt customerExt = new CustomerExt();
		customerExt.setId(id);
//		customerExt.setDelete(DeleteEnum.DETELED.getCode());
		int count = customerExtManager.update(customerExt);
		/** 3 返回对象 */
		return count;
    }

	/**
	 * 根据条件查询对象对象list列表
	 * @param request
	 * @return 查询结果
	 */
	@Override
	public List<CustomerExt> getListByParam(CustomerExt request){
		/** 1 验证请求参数 */
		//可以设置排序
		/** 2 处理业务 */
		List<CustomerExt> queryList = customerExtManager.getListByParam(request);
		/** 3 返回结果 */
		if (CollectionUtils.isNotEmpty(queryList)) {
		return BeanUtil.copyByList(queryList, CustomerExt.class);
		}
		return new ArrayList<>();
	}

	/**
	 * 根据条件查询对象list记录
	 * @param request
	 * @return 分页对象
	 */
	@Override
	public PageResponse<CustomerExt> getPageByParam(PageRequest<CustomerExt> request){
		/** 1 验证请求参数 */
		ValidateUtils.checkObjNull(request, "请求参数不能为空");
		//可以设置排序 query.orderByCreatedTime(true);
		/** 2 处理业务 */
		RowBounds rowBounds = new RowBounds(request.getPageNum(), request.getPageSize());
		Page<CustomerExt> queryPageList = customerExtManager.getPageByParam(request.getParamData(), rowBounds);
		PageResponse<CustomerExt> pageResponse = new PageResponse<>();
		pageResponse.converPageResponse(pageResponse, queryPageList, request);
		pageResponse.setDataList(BeanUtil.copyByList(queryPageList.getResult(), CustomerExt.class));
		/** 3 返回结果 */
		return pageResponse;
	}

	/**
	 * 根据条件查询单个对象
	 * @param request
	 * @return
	 */
	@Override
	public CustomerExt getOneByParam(CustomerExt request) {
		return customerExtManager.getOneByParam(request);
	}


	@Override
	public Long insertOrUpdateCustomerExt(CustomerExt customerExt) {
		ValidateUtils.checkObjNull(customerExt, "参数错误");
		ValidateUtils.checkLongGTZero(customerExt.getCusId(), "客户扩展信息中客户主键不存在");

		//保存客户标签信息 bef_customer_ext
		CustomerExt queryExt = new CustomerExt();
		queryExt.setCusId(customerExt.getCusId());
		queryExt.setDeleted(DeleteEnum.INIT.getFlag());
		queryExt.setCustomerType(customerExt.getCustomerType());
		queryExt.setDealerId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_DEALER_ID.getCode()));
		queryExt.setCorpId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_CORP_ACCOUNTID.getCode()));
		queryExt.setTenantId(SearchLocalHeader.getHeader(SCRMRequestHeaderEnum.HEADER_TENANTID.getCode()));

		CustomerExt existCustomerExt = customerExtManager.getOneByParam(queryExt);
		if (EmptyUtils.isEmpty(existCustomerExt)) {
		    customerExt = CustomerExtAssembler.initAddCustomerExt(customerExt);
			int insertResult = customerExtManager.insert(customerExt);
			if (insertResult <= 0) {
				throw new BusinessException(NEError.BUSINESS_ERROR, "保存总部客户扩展信息失败");
			}
			return customerExt.getId();
		}
		customerExt.setId(existCustomerExt.getId());
		customerExt = CustomerExtAssembler.initUpdateCustomerExt(customerExt);
		int updateResult = customerExtManager.update(customerExt);
		if (updateResult <= 0) {
			throw new BusinessException(NEError.BUSINESS_ERROR, "更新总部客户扩展信息失败");
		}
		return existCustomerExt.getId();
	}
}
